package com.hxzy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.common.vo.R;
import com.hxzy.entity.Merchandise;
import com.hxzy.service.ContamerService;
import com.hxzy.service.MerchandiserService;
import com.hxzy.service.OrdersService;
import com.hxzy.service.ProductService;
import com.hxzy.vo.ReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Api(tags = "首页看板API")
@RequestMapping(value = "/api/dashboard")
@RestController
public class DashboardController extends BaseController {

    /**
     * 会员
     */
    @Autowired
    private ContamerService contamerService;

    /**
     * 商品
     */
    @Autowired
    private ProductService productService;

    /**
     * 订单
     */
    @Autowired
    private OrdersService ordersService;

    /**
     * 商家
     */
    @Autowired
    private MerchandiserService merchandiserService;


    @ApiOperation(value = "面板数据")
    @GetMapping(value = "/data")
    public R dashboardData(){

        //会员总数量 allNum
        long allNum=this.contamerService.count();

        //菜品总数量 countProductNum(管理员和商家查询数量不同)
        long countProductNum=0;
        if(WaimaiThreadLocal.isSuperMan()){
            //select count(*) from merchandise
            countProductNum=this.productService.count();
        }else{
            //select count(*) from merchandise where merchant_id=?
            QueryWrapper<Merchandise> query=new QueryWrapper<>();
            query.eq("merchant_id", WaimaiThreadLocal.getCurrent().getAccountVO().getId());
            countProductNum=this.productService.count(query);
        }

        //成交总金额 totalMoney (全年)
        long totalMoney=this.ordersService.fullYearTotalMoney();

        //全年交易笔数 totalOrders
        long totalOrders=this.ordersService.fullYearTotalOrders();

        //待审核的商家数（管理员） notApprovedNum
        long notApprovedNum=this.merchandiserService.notApprovedNum();

        //本月新注册会员数  currentMonthNum
        long currentMonthNum= this.contamerService.currentMonthRegister();

        //未成交的订单数
        long outstandingOrders=this.ordersService.outstandingOrders();

        Map<String,Object> data=new HashMap<>();
        data.put("allNum",allNum);
        data.put("countProductNum",countProductNum);
        data.put("totalMoney",totalMoney);
        data.put("totalOrders",totalOrders);
        data.put("notApprovedNum",notApprovedNum);
        data.put("currentMonthNum",currentMonthNum);
        data.put("outstandingOrders",outstandingOrders);

        return R.okHasData(data);
    }

    @ApiOperation(value = "统计会员注册数")
    @GetMapping(value = "/contamer/report/{range}")
    @ApiImplicitParam(name = "range",value = "范围(1 本周，2本月，3全年)",required = true)
    public R contamerLineBar( @PathVariable(value = "range") int range){
        //查询结果
        List<ReportVO> arr= this.contamerService.contamerLineBar(range);

        //标题
        String[] xTitle=new String[arr.size()];
        Object[] series=new Object[arr.size()];
        for(int i=0;i<arr.size();i++){
            ReportVO vo=arr.get(i);
            xTitle[i]=vo.getLabel();
            series[i]=vo.getData();
        }

        Map<String,Object> data=new HashMap<>();
        data.put("xAxis",xTitle);
        data.put("series",series);
        return R.okHasData(data);
    }

    @ApiOperation(value = "统计商家注册数")
    @GetMapping(value = "/merchandiser/report")
    public R merchandiserLineBar(){
        //查询结果
        List<ReportVO> arr= this.merchandiserService.monthlyNumers();

        //标题
        String[] xTitle=new String[arr.size()];
        Object[] series=new Object[arr.size()];
        for(int i=0;i<arr.size();i++){
            ReportVO vo=arr.get(i);
            xTitle[i]=vo.getLabel();
            series[i]=vo.getData();
        }

        Map<String,Object> data=new HashMap<>();
        data.put("xAxis",xTitle);
        data.put("series",series);
        return R.okHasData(data);
    }
}
